#include "fractals.h"

Fractals::Fractals(int length = 400) : _l(length){}

Fractals::~Fractals()
{
	delete [] _m;
	delete [] _p;
}
void Fractals::ReadFractals(*char filename)
{
	FILE *input = fopen(filename,"r");
	assert(input != NULL);

	fscanf(input,"%d",&_n);

	_m = new Matrix[num_transforms];

	_p = new float[num_transforms];

	for (int i = 0; i < _n; i++) {
		fscanf (input,"%f",&_p[i]);
		_m[i].Read3x3(input);
	}

	for (int i =  _n - 1; i > 0; i--)
		for(int j = 0; j < i; j++)
			_p[i] += _p[j];

	fclose(input);
}

void Fractals::WriteImage(char* filename)
{
	srand((unsigned)time(0));

	Image img(_l, _l);

	Matrix po;

	for(int i = 0; i < 100000; i++)
	{
		po.Set(0, 0, rand() % 200);
		po.Set(0, 1, rand() % 200);
		po.Set(0, 3, 200);

		for(int k = 0; k < 4; k++)
		{
			float x = float(rand() % 100) / 100;

			int fi;// = rand() % 3;
			for(int n = 0; n < _n; n++)
			{
				if ( x < _p[n])
				{
					fi = n;
					break;
				}
			}
			po = _m[fi] * po;
		}
		Vec3f v(po.Get(0, 0), po.Get(0, 1), po.Get(0, 3));
		//dc.SetPixelV(v.x() + 200, v.y() + 200, RGB(v.r(), v.g(), v.b()));
		img.SetPixel(po.Get(0, 0), po.Get(0, 1), v);
		//dc.SetPixelV((int)po.Get(0, 0), (int)po.Get(0, 1), RGB(100, 0, 0));
	}


	img.SavePPM(filename);
}